package com.nuoniu.sibanyun.service.crm.call.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.config.YmlConfig;
import com.nuoniu.sibanyun.entity.crm.call.CrmCallVoice;
import com.nuoniu.sibanyun.entity.crm.call.dto.CrmCallVoiceSearchDto;
import com.nuoniu.sibanyun.mapper.crm.call.CrmCallVoiceMapper;
import com.nuoniu.sibanyun.service.crm.call.ICrmCallVoiceService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import com.nuoniu.sibanyun.utils.FileUtils;
import com.nuoniu.sibanyun.utils.entity.UploadFileResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 客户管理-外呼管理-语音 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2022-02-28
 */
@Service
public class CrmCallVoiceServiceImpl extends ServiceImpl<CrmCallVoiceMapper, CrmCallVoice> implements ICrmCallVoiceService {


    @Override
    public Page<CrmCallVoice> pageList(CrmCallVoiceSearchDto crmCallVoiceSearchDto) {
        UserInfo userInfo = UserThreadLocal.get();
        Page<CrmCallVoice> page = crmCallVoiceSearchDto.getPage();
        QueryWrapper<CrmCallVoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("create_user_id",userInfo.getUserId());
        return page(page,queryWrapper);
    }

    @Override
    public boolean getByName(String name, Long oldId) {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<CrmCallVoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("create_user_id",userInfo.getUserId());
        queryWrapper.eq("voice_name",name);
        if(null != oldId){
            queryWrapper.ne("id",oldId);
        }
        return count(queryWrapper) > 0;
    }

    @Override
    public void saveVoice(CrmCallVoice crmCallVoice) {
        boolean isHaveName = this.getByName(crmCallVoice.getVoiceName(), crmCallVoice.getId());
        if(isHaveName)throw new NuoNiuException("名称不能重复");
        this.saveOrUpdate(crmCallVoice);
    }

    @Override
    public List<CrmCallVoice> uploadVoice(MultipartFile[] file) {
        LocalDateTime now = LocalDateTime.now();
        List<CrmCallVoice> crmCallVoiceList = new ArrayList<>();
        UserInfo userInfo = UserThreadLocal.get();
        Integer companyId = userInfo.getCompanyId();
        Integer userId = userInfo.getUserId();
        String realName = userInfo.getRealName();

        if(file == null || file.length == 0) {
            throw new NuoNiuException("文件不能为空");
        }
        String classDir = "voice/"+ companyId +"/" + userId +"/";
        List<UploadFileResult> resultList = FileUtils.uploadFileResult(file, classDir);
        resultList.forEach(item->{
            String uploadPath = YmlConfig.DOWNLOAD_FILE_URL + item.getFilePath();
            CrmCallVoice crmCallVoice = new CrmCallVoice();
            crmCallVoice.setCompanyId(companyId);
            crmCallVoice.setCreateUserId(userId);
            crmCallVoice.setCreateUserName(realName);
            crmCallVoice.setCreateDate(now);
            crmCallVoice.setVoiceUrl(uploadPath);
            crmCallVoice.setVoiceName(item.getFileName());
            crmCallVoiceList.add(crmCallVoice);
        });
        this.saveBatch(crmCallVoiceList);
        return crmCallVoiceList;
    }

    @Override
    public void delete(Long id) {
        if(null == id || id < 1)throw new NuoNiuException("参数错误");
        CrmCallVoice oldCrmCallVoice = getById(id);
        if(null == oldCrmCallVoice)throw new NuoNiuException("语音记录不存在");
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper<CrmCallVoice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",userInfo.getCompanyId());
        queryWrapper.eq("id",id);
        this.remove(queryWrapper);
    }
}
